import * as API from '../../../api/contactsApi';
import * as util from '../../../utils/util';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchkey: '',
    pageNo: 1,
    pageSize: 20,
    total: 0,
    showRecentSearch: true,
    recentSearchRecord: [],
    dataSource: [],
    showContactsFinishedDialog: false,
    showContactsCancelDialog: false,
    curContactsId: '',
    curitemIdx: 0,
    hasRequest: false
  },

  //前往详情页面
  gotoDetail: function (e) {
    util.preventMoreClick(this, () => {
      const id = e.currentTarget.dataset.id;
      wx.navigateTo({ url: '../contactsDetail/contactsDetail?id=' + id });
    })
  },

  setSearchKey: function (value) {
    this.setData({
      searchkey: value || ''
    })
  },
  searchRecordClick: function (e) {
    this.setSearchKey(e.target.dataset.text);
    this.setData({
      pageNo: 1,
      pageSize: 20
    })
    this.addRecentRecord();
    this.loadData(true);
  },
  search(e) {
    // let searchkey = e.detail || (this.data.recentSearchRecord[0] && this.data.recentSearchRecord[0].content);
    let searchkey = e.detail
    if (!searchkey){
      wx.showToast({
        title: '请输入搜索关键字',
        icon:"none"
      })
    }else{
      this.setData({
        pageNo: 1,
        pageSize: 20,
        searchkey: searchkey,
        dataSource: []
      })
      this.loadData(true);
      if (e.detail)
        this.addRecentRecord();
    }
  },
  hideRecentRecord: function (e) {
    this.showRecentRecord(e.detail);
  },
  clearSearch(e) {
    this.showRecentRecord(e.detail);
  },
  showRecentRecord: function (str) {
    if (str.length == 0) {
      this.setData({
        dataSource: [],
        showRecentSearch: true,
        hasRequest: false
      })
      this.getRecentRecord();
    } else if (str == 'false')
      this.setData({
        recentSearchRecord: [],
        showRecentSearch: false
      })

  },

  clearRecentRecord: function () {
    API.clearSearchRecord().then(res => {
      this.showRecentRecord('false');
      this.getRecentRecord();
    }, err => {

    })
  },
  addRecentRecord: function () {
    API.addSearchRecord({ content: this.data.searchkey }).then(res => {

    }, err => {

    })
  },
  getRecentRecord: function () {
    API.getRecentSearchContacts().then(res => {
      this.setData({
        recentSearchRecord: res.customList,
        showRecentSearch: true
      })
    }, err => {

    })
  },
  loadData: function (options) {
    const that = this;
    const postBody = {
      searchKey: this.data.searchkey,
      pageNo: this.data.pageNo,
      pageSize: this.data.pageSize
    };
    this.setData({
      hasRequest: false
    })
    this.showRecentRecord('false');
    API.contactsList(postBody).then(res => {
      let dataList = res.customAccountList || [];
      if (!options) {
        dataList = that.data.dataSource.concat(res.customAccountList);
      }
      let hasMore = dataList.length < res.totalCount
      that.setData({
        isShowReloadBtn: false,
        dataSource: dataList,
        pageNo: that.data.pageNo,
        total: res.totalCount,
        hasMore: hasMore,
        hasRequest: true
      });
    }, err => {
      that.setData({
        isShowReloadBtn: true
      });
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options&&options.detail){
      this.setData({
        searchKey: options.detail,
      })
      this.search(options);
    }else{
      this.getRecentRecord();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    if (hasMore) {
      this.setData({
        pageNo: ++this.data.pageNo
      });
      this.loadData(false);
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var hasMore = this.data.dataSource.length < this.data.total;
    if (hasMore) {
      this.setData({
        pageNo: ++this.data.pageNo
      });
      this.loadData(false);
    }
  }
})